import { mapState, mapGetters } from 'vuex'
import { DEFAULT_PAGE_SIZE } from '@/const'
import { resourceApi } from '@/request'
import { sleep } from '@/utils'
const RESOURCE_TYPES_MAP = {
    1: '公共',
    2: '私有',
    3: '共享'
}

// 是否是页码变化
let IS_PAGE_SIZE_CHANGE = false

let MS = 0

export default {
    data() {
        return {
            searchWord: '',
            resourceLabel: RESOURCE_TYPES_MAP[1],
            loading: false,
            isRefreshing: false,  // 是否正在同步资源
            breadcrumbList: [ ],
            tableData: [ ],
            pageSize: DEFAULT_PAGE_SIZE,
            total: 0,
            currentPage: 1,
            dialogVisible: false,
            detailVisible: false,
            currentRow: {},
        }
    },
    computed: {
        ...mapState([ 'loginerName' ]),
        resourceType() {
            const types = Object.keys(RESOURCE_TYPES_MAP)
            const item = types.find(type => RESOURCE_TYPES_MAP[type] === this.resourceLabel) || {}
            return Number(Object.values(item)[0]) || 1
        }
        // ...mapGetters([ 'isManager', 'isTenant', 'isUser' ]),
    },
    mounted() {
        // 初始化资源列表
        this.getResourceList()
    },
    methods: {
        // 获取资源数据
        async getResourceList(info = {}) {
            const service = this.$route.meta.service
            if (!service) {
                return false
            }
            const apiName = `get${service}List`
            const { currentPage, pageSize, resourceId = 0 } = info
            this.loading = true
            const params = {
                pageIndex: currentPage || this.currentPage,
                pageSize: pageSize || this.pageSize,
                reqParam: {
                    type: this.resourceType,
                    resourceId,
                }
            }
            if (info.resourceName) {
                params.reqParam.resourceName = info.resourceName
            } else {
                this.searchWord = ''
            }
            try {
                const res = await resourceApi[apiName](params)
                this.currentPage = params.pageIndex
                this.pageSize = params.pageSize
                this.tableData = res.pageList || []
                this.total = res.count || this.tableData.length
                if (res.pageList && res.pageList.length === 0 && params.pageIndex > 1) {
                    this.getResourceList({...info, currentPage: params.pageIndex - 1})
                }
            } finally {
                await sleep(100)
                this.loading = false
                if (IS_PAGE_SIZE_CHANGE) {
                    this.$nextTick(() => {
                        IS_PAGE_SIZE_CHANGE = false
                    })
                }
            }
        },
        // 点击面包屑的某一项
        onBreadChange(bread, index) {
            this.breadcrumbList = this.breadcrumbList.slice(0, index + 1)
            const service = this.$route.meta.service || 'HDFS'
            if (/kafka/i.test(service)) {
                return
            }
            this.getResourceList({ resourceId: bread.resourceId, currentPage: 1 })
        },
        // 点击返回
        onReturn() {
            this.breadcrumbList = []
            this.getResourceList({ currentPage: 1 })
        },
        // 点击分配权限
        handleAssignAuth(column) {
            this.currentRow = column
            this.dialogVisible = true
        },
        // 点击查看详情
        handleViewDetail(column) {
            this.currentRow = column
            this.detailVisible = true
        },
        // pageSize变化的回调
        handlePageSizeChange(pageSize) {
            IS_PAGE_SIZE_CHANGE = true
            const param = { 
                currentPage: 1, 
                pageSize, 
                resourceId: this.currDir.resourceId 
            }
            if (this.searchWord) {
                param.resourceName = this.searchWord
            }
            this.getResourceList(param)
        },
        // currentPage变化的回调
        handleCurrentPageChange(index) {
            if (IS_PAGE_SIZE_CHANGE) {
                return
            }
            const param = { 
                currentPage: index,
                resourceId: this.currDir.resourceId
            }
            if (this.searchWord) {
                param.resourceName = this.searchWord
            }
            this.getResourceList(param)
        },
        formatDian(length, i) {
            return length > 1 && i < length - 1 ? ',' : ''
        },
        // 同步资源
        onRefresh() {
            const now = Date.now()
            if (now - MS < 1000) {
                return false
            }
            MS = now
            const serviceName = this.$route.meta.service
            if (!serviceName) {
                return false
            }
            const apiName = `sync${serviceName}`
            const param = {}
            const resourceId = this.currDir.resourceId || 0
            if (/yarn/i.test(serviceName)) {
                param.level = this.currDir.level || 0
                param.resourceId = resourceId
            } else if (/hive/i.test(serviceName)) {
                param.resourceId = resourceId
            }
            this.isRefreshing = true
            resourceApi[apiName](param).then(() => {
                this.handleUpdateAuth()
                this.isRefreshing = false
            }).catch(() => {
                this.isRefreshing = false
            })
        },
        // 更新权限列表
        handleUpdateAuth() {
            const param = {
                resourceId: this.currDir.resourceId
            }
            if (this.searchWord) {
                param.resourceName = this.searchWord
            }
            this.getResourceList(param)
        },
        // 搜索
        onSearch(word) {
            const param = {
                currentPage: 1,
                resourceId: this.currDir.resourceId
            }
            if (word) {
                param.resourceName = word
            }
            this.getResourceList(param)
        },
    },
}